//
//  LCFirstVideoViewController.m
//  TheLCNews
//
//  Created by apple on 15/12/4.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LCFirstVideoViewController.h"
#import <MediaPlayer/MediaPlayer.h>


@interface LCFirstVideoViewController ()
@property (nonatomic ,strong) MPMoviePlayerController *moviePlayerViewController;


@end

@implementation LCFirstVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.moviePlayerViewController.view];
    CGFloat hei = WIDTH / (HEIGHT / WIDTH );
    [self.moviePlayerViewController.view setFrame:CGRectMake(0, 0, WIDTH, hei+15)];
    self.moviePlayerViewController.view.center = self.view.center;

    self.moviePlayerViewController.controlStyle = 0;
    
}
-(void)dealloc{
    //移除所有通知监控
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 *  取得本地文件路径
 *
 *  @return 文件路径
 */
-(NSURL *)getFileUrl{
    NSString *path  = [[NSBundle mainBundle] pathForResource:@"Welcome" ofType:@"mov"];
    NSURL *URL = [NSURL fileURLWithPath:path];
    return URL;
}

- (void)showGuidePage:(NSArray *)imageArr{
    CGRect size = [UIScreen mainScreen].bounds;
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:size];
    scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:scrollView];
    [self.view sendSubviewToBack:scrollView];
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.contentSize = CGSizeMake(imageArr.count * size.size.width, 0);
    for (NSInteger i = 0; i < imageArr.count; i++) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * size.size.width, 0, size.size.width, size.size.height)];
        [scrollView addSubview:imageView];
        imageView.image = [UIImage imageNamed:[imageArr objectAtIndex:i]];
        
    }
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTintColor:[UIColor blackColor]];
    [button setTitle:@"立即体验" forState:UIControlStateNormal];
    button.frame = CGRectMake( (imageArr.count * size.size.width) - (size.size.width/ 2) - 40  , size.size.height - 60, 80, 40);
    button.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
    button.layer.cornerRadius = 10;
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:button];
    
}


- (MPMoviePlayerViewController *)moviePlayerViewController{
    if (!_moviePlayerViewController) {
        NSURL *url=[self getFileUrl];
        _moviePlayerViewController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
        
        [self addNotification];
    }
    return _moviePlayerViewController;
}
//#pragma mark - UI事件
//- (IBAction)playClick:(UIButton *)sender {
//    self.moviePlayerViewController=nil;//保证每次点击都重新创建视频播放控制器视图，避免再次点击时由于不播放的问题
//    //    [self presentViewController:self.moviePlayerViewController animated:YES completion:nil];
//    //注意，在MPMoviePlayerViewController.h中对UIViewController扩展两个用于模态展示和关闭MPMoviePlayerViewController的方法，增加了一种下拉展示动画效果
//    [self presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];
//}
//
#pragma mark - 控制器通知
/**
 *  添加通知监控媒体播放控制器状态
 */
-(void)addNotification{
//    NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];
//    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayerViewController.moviePlayer];
//    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayerViewController.moviePlayer];
    
}

/**
 *  播放状态改变，注意播放完成时的状态是暂停
 *
 *  @param notification 通知对象
 */
-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
    switch (self.moviePlayerViewController.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放...");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停播放.");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止播放.");
            break;
        default:
//            NSLog(@"播放状态:%li",self.moviePlayerViewController.moviePlayer.playbackState);
            break;
    }
}

/**
 *  播放完成
 *
 *  @param notification 通知对象
 */
-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
    NSLog(@"播放完成.%li",self.moviePlayerViewController.playbackState);
    [self click];
    
    
}
//实现横屏



- (void)click{
    
    
    [UIView animateWithDuration:1 animations:^{
        self.view.alpha = 0;
        
        
    } completion:^(BOOL finished) {
        
//        self.block();
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
